package com.cardbaobao.cardbabyclient.controller;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.base.App;
import com.cardbaobao.cardbabyclient.model.UserInfo;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.utils.SharedPreferencesUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/1/9.
 * 初始化controller
 */

public class InitController {
    private Context mContext;
    private App mApplication;

    public InitController(Context context) {
        mContext = context;
        mApplication = ApplicationHelper.getApplication();
    }


    /**
     * 更新用户信息
     */
    public synchronized void updateUserInfo() {
        if (mApplication.getIsLogin()&&mApplication.getUser()!=null) {
            Observable<String> observable=
                    Api.getUserInfo(String.valueOf(mApplication.getUser().getID()));
            observable.subscribe(new Subscriber<String>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(String s) {
                    JSONObject object=JSON.parseObject(s);
                    if(object!=null&&object.getIntValue("code")==1){
                        UserInfo userInfo =
                                JSON.parseObject(object.getJSONObject("msg").getString("data"),
                                        UserInfo.class);
                        if (userInfo != null) {
                            userInfo.setPhone(mApplication.getUser().getPhone());
                            mApplication.setUser(userInfo);
                            new BroadcastController(mContext).notifyUserLogin();
                        }
                    }
                }
            });
        }
        if(TextUtils.isEmpty(SharedPreferencesUtil.PhoneInfo.getTemporaryId())){
            SelfUtils.post(URLUtil.TEMPORARY_ID_URL,null,null).
                    subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
                    subscribe(new Subscriber<String>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(String s) {
                            if(!TextUtils.isEmpty(s)){
                                SharedPreferencesUtil.PhoneInfo.setTemporaryId(s);
                            }
                        }
                    });
        }
    }

}
